package com.example.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.example.web.entity.User;
import com.example.web.service.UserService;
import com.example.web.tool.R;

@RestController
public class UserController {

	@Autowired
	UserService userService;
	
	@PostMapping("/user")
	public R save(User user) {
		Integer uint = userService.save(user);
		return R.ok("success").put("data", uint);
	}
	
	@DeleteMapping("/user{id}")
	public R remove(@PathVariable Integer id) {
		Integer uint = userService.remove(id);
		return R.ok("success").put("data", uint);
	}
	
	@PutMapping("/user")
	public R change(User user) {
		Integer uint = userService.change(user);
		return R.ok("success").put("success", uint);
	}
	
	@GetMapping("/user{id}")
	public R getUser(@PathVariable Integer id) {
		User u = userService.getUser(id);
		return R.ok("success").put("data", u);
	}
	
	@RequestMapping(value="/userList",method=RequestMethod.GET)
	public R getList() {
		List<User> us = userService.getList();
		return R.ok("success").put("data", us);
	}
}
